package com.study.utils;

import com.study.config.FastDFSConfig;
import com.study.config.FastDFSConfigInitializer;
import com.study.entity.dto.FastDFSFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * FastDFS 客户端工具类
 */
@Component
public class FastDFSClient {

    private static final Logger logger = LoggerFactory.getLogger(FastDFSClient.class);


    @Autowired
    private FastDFSConfig fastDFSConfig;

    @Autowired
    private FastDFSConfigInitializer fastDFSConfigInitializer;

    /**
     * 文件上传
     *
     * @param file FastDFSFile 对象，包含文件内容、扩展名和作者信息
     * @return 上传结果数组，包含组名和文件存储路径
     */
    public String[] upload(FastDFSFile file) {
        NameValuePair[] metaList = new NameValuePair[1];
        metaList[0] = new NameValuePair("author", file.getAuthor());

        String[] uploadResults = null;
        StorageClient storageClient = null;
        try {
            storageClient = fastDFSConfigInitializer.getStorageClient();
            uploadResults = storageClient.upload_file(file.getContent(), file.getExt(), metaList);
        } catch (Exception e) {
            logger.error("Exception when uploading the file: " + file.getName(), e);
        } finally {
            if (uploadResults == null && storageClient != null) {
                logger.error("Upload file fail, error code: " + storageClient.getErrorCode());
            }
        }
        return uploadResults;
    }

    /**
     * 获取文件信息
     *
     * @param remoteFileName 文件存储完整名
     * @return FileInfo 对象，包含文件信息
     */
    public FileInfo getFileInfo(String remoteFileName) {
        try {
            StorageClient storageClient = fastDFSConfigInitializer.getStorageClient();
            return storageClient.get_file_info(fastDFSConfig.getGroupName(), remoteFileName);
        } catch (Exception e) {
            logger.error("Exception: Get File from Fast DFS failed", e);
        }
        return null;
    }

    /**
     * 文件下载
     *
     * @param remoteFileName 文件存储完整名
     * @return 文件的 InputStream
     */
    public InputStream downloadFile(String remoteFileName) {
        try {
            StorageClient storageClient = fastDFSConfigInitializer.getStorageClient();
            byte[] fileBytes = storageClient.download_file(fastDFSConfig.getGroupName(), remoteFileName);
            return new ByteArrayInputStream(fileBytes);
        } catch (Exception e) {
            logger.error("Exception: Get File from Fast DFS failed", e);
        }
        return null;
    }

    /**
     * 文件删除
     *
     * @param remoteFileName 文件存储完整名
     * @return 删除结果，0 表示成功
     * @throws Exception 删除失败时抛出异常
     */
    public int deleteFile(String remoteFileName) throws Exception {
        StorageClient storageClient = fastDFSConfigInitializer.getStorageClient();
        return storageClient.delete_file(fastDFSConfig.getGroupName(), remoteFileName);
    }

}
